<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use Session,Config;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Expert\ExpertService;
use Illuminate\Pagination\LengthAwarePaginator;

class IndexController extends Controller
{
    private $index = 'Expert.index';
    private $summary = 'Expert.summary';
    private $detail = 'Expert.detail';

    public function index()
    {
        return view($this->index);
    }

    /**
     * @return $this
     * 主页
     */
    public function summary(Request $request)
    {
        $where = [];
        if($request->has('start_date')){
            $where['start_date'] = $request->input('start_date');
        }
        if($request->has('end_date')){
            $where['end_date'] = $request->input('end_date');
        }
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $expert =  new ExpertService();
        $data = $expert->Expert($where);
        if($data['error_code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }

        return view($this->summary)->with('data',$data)->withInput($request->flashOnly('start_date','end_date'));;
    }


    public function detail(Request $request)
    {
        $where = [];
        $where['course_id'] = $request->input('course_id');
        $where['page_no'] = $request->input('page');
        $where['start_date'] = $request->input('start_date');
        $where['end_date'] = $request->input('end_date');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

//        dd($where);
        $expert =  new ExpertService();
        $data = $expert->Detail($where);

        if($data['error_code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }
        $data['where'] = $where;

        $total = $data['num'];
//        dd($data);
        $perPage = 10;
        $items = array_slice($data,0);
        $data =  new LengthAwarePaginator($items,$total,$perPage);
        $data->setPath('/course');
        return view($this->detail)->with('data',$data)->withInput($request->flashOnly('start_date','end_date'));
    }



}
